const http = require('http');
const server = http.createServer();
server.listen(3000,()=>{
  console.log('服务器已启动');
});

const fs = require('fs');
const path = require('path');

server.on('request',(req,res)=>{
  //res是响应对象，在该对象上新增一个sendFile的方法
  res.sendFile = function(fpath){
    fs.readFile(path.join(__dirname,fpath),(err,data)=>{
      if(err){
        console.log(err);
        return res.end('404 not found');
      }
       res.end(data);
    });
  };
  const url = req.url;
  console.log(url);
  if(url ==='/index'){
    res.sendFile('/view/index.html');

  }else if(url ==='/phone'){
    res.sendFile('/view/phone.html');

  }else if(url === '/admin/index'){
    res.sendFile('view/admin/index.html');
   
  }else if(url === '/admin/login'){
    res.sendFile('view/admin/login.html');

  }else if(url.startsWith('/public')){
    res.sendFile(url);

  }else{
    res.setHeader('content-type','text/html;charset=utf-8');
    res.end('此网页不存在');
  };
});